﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;
using System.Windows.Forms;

namespace MyMedia
{
    public class MediaPlayer
    {
        [DllImport("winmm.dll")]
        private static extern int mciSendString(string strCommand, StringBuilder strReturn, int iReturnLength, IntPtr hwndCallback);
        [DllImport("winmm.dll")]
        private static extern int mciGetErrorString(int errCode, StringBuilder errMsg, int buflen);

        private StringBuilder msg = new StringBuilder(128);  // MCI Error message
        private StringBuilder returnData = new StringBuilder(128);  // MCI return data
        private int error;
        private bool IsOpened = false;
        private bool IsPlaying = false;

        public bool Open(string FileName)
        {
            Close();
            error = mciSendString("Open \"" + FileName + "\" type mpegvideo alias MyMediaFile", null, 0, IntPtr.Zero);
            if (error != 0)
            {
                error = mciSendString("open \"" + FileName + "\" alias MediaFile", null, 0, IntPtr.Zero);
                if (error == 0)
                {
                    IsOpened = true;
                    return true;
                }
                else
                {
                    IsOpened = false;
                    return false;
                }
            }
            else
            {
                IsOpened = true;
                return true;
            }
        }
        public bool Close()
        {
            if (IsOpened)
            {
                error = mciSendString("Close MyMediaFile", null, 0, IntPtr.Zero);
                if (error == 0)
                {
                    IsPlaying = false;
                    return true;
                }
                else
                {
                    return false;
                }
            } else {
                return false;
            }
        }
        public bool Play()
        {
            if (IsOpened)
            {
                error = mciSendString("Play MyMediaFile", null, 0, IntPtr.Zero);
                if (error == 0)
                {
                    IsPlaying = true;
                    return true;
                }
                else
                {
                    Close();
                    return false;
                }
            }
            else
            {
                return false;
            }
        }
        public void Pause()
        {
            if (IsPlaying)
            {
                error = mciSendString("Pause MyMediaFile", null, 0, IntPtr.Zero);
            }
        }
        public bool Stop()
        {
            if (IsOpened)
            {
                if (IsPlaying)
                {
                    error = mciSendString("Stop MyMediaFile", null, 0, IntPtr.Zero);
                    if (error == 0)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }
        public void SetPosition(int miliseconds)
        {
            if (IsPlaying)
            {
                error = mciSendString("play MyMediaFile from " + miliseconds.ToString(), null, 0, IntPtr.Zero);
            }
            else
            {
                error = mciSendString("seek MyMediaFile to " + miliseconds.ToString(), null, 0, IntPtr.Zero);
            }
        }
        public bool SetVolume(int volume)
        {
            if (volume >= 0 && volume <= 1000)
            {
                error = mciSendString("setaudio MyMediaFile volume to " + volume.ToString(), null, 0, IntPtr.Zero);
                return true;
            }
            else
            {
                return false;
            }
        }
        public int GetSongLength()
        {
            if (IsPlaying)
            {
                error = mciSendString("status MyMediaFile length", returnData, returnData.Capacity, IntPtr.Zero);
                return int.Parse(returnData.ToString());
            }
            else
            {
                return 0;
            }
        }
        public double Duration()
        {
            double ReturnSeconds;
            mciSendString("status MyMediaFile length", msg, 128, IntPtr.Zero);

            ReturnSeconds = Convert.ToDouble(msg.ToString());
            ReturnSeconds = ReturnSeconds / 1000;
            return ReturnSeconds;
        }
        public int GetCurrentMilisecond()
        {
            mciSendString("Status MyMediaFile position", returnData, returnData.Capacity, IntPtr.Zero);
            return int.Parse(returnData.ToString());
        }
        public int GetCurrentSecond()
        {
            mciSendString("Status MyMediaFile position", returnData, returnData.Capacity, IntPtr.Zero);
            return TimeSpan.FromMilliseconds(int.Parse(returnData.ToString())).Seconds;
        }
    }

    public class PositionChangedEventArgs : EventArgs
    {
        private int _position;
        public PositionChangedEventArgs(int num)
        {
            this._position = num;
        }
        public int newPosition
        {
            get
            {
                return _position;
            }
        }
    }
}
